package com.imessage.action.no.group;

import com.bean.BusinessGroup;
import com.bean.BusinessUsePhone;
import com.imessage.comm.Constants;
import com.imessage.vo.GroupVo;
import com.imessage.vo.UsePhoneVo;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/group")
public class GroupAction extends GroupBaseAction {

    @RequestMapping(value = "")
    public Object run(GroupVo groupVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, groupVo);
        Page<BusinessGroup> page = baseService.findPage(BusinessGroup.class, expressionList,
                null, getOrder(groupVo.getOrders()), groupVo.getPageCurrent(),
                groupVo.getPageSize());
        return page;
    }

    @RequestMapping(value = "initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessGroup group = new BusinessGroup();
        if (StringUtils.isNotBlank(id)) {
            group = baseService.findById(BusinessGroup.class, id);
            modelAndView.setViewName("admin/no/group/modify");
        } else {
            modelAndView.setViewName("admin/no/group/add");
        }
        setModelAndView(modelAndView, group);
        return modelAndView;
    }


    @RequestMapping(value = "add")
    public Object add(GroupVo groupVo) {
        BusinessGroup group = new BusinessGroup();
        setBusinessGroup(groupVo, group);
        int state = baseService.saveObject(group);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


    @RequestMapping(value = "modify")
    public Object modify(GroupVo groupVo) {
        BusinessGroup group = baseService.findById(BusinessGroup.class, groupVo.getId());
        setBusinessGroup(groupVo, group);
        int state = baseService.updateObject(group);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return group;
        }
        return getJsonForErr("", datagrid);
    }

    @RequestMapping(value = "del")
    public Object del(@RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessGroup group = baseService.findById(BusinessGroup.class, id);
            state = baseService.delObject(group);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


}
